//
//  UIAlertController+CZExtension.m
//  IntechSmartCampus
//
//  Created by awh on 2017/10/19.
//  Copyright © 2017年 awh. All rights reserved.
//

#import "UIAlertController+CZExtension.h"

@implementation UIAlertController (CZExtension)

+ (UIViewController *)findAppreciatedRootVC {
    UIViewController *rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    if ([rootVC presentedViewController] != nil) {
        rootVC = rootVC.presentedViewController;
    }
    return rootVC;
}

+ (void)presentAlertController:(UIAlertController *)alertController {
    UIViewController *rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    if ([rootVC presentedViewController] != nil) {
        rootVC = [UIAlertController findAppreciatedRootVC];
    }
    [rootVC presentViewController:alertController animated:YES completion:nil];
}

+ (void)presentAlertControllerWithTitle:(NSString *)title message:(NSString *)message doneBlock:(void (^)())block {
    [self presentAlertControllerWithTitle:title message:message doneTitle:@"确定" cancelTitle:@"取消" completeBlock:^(BOOL isCancelled) {
        if (isCancelled) {
        } else {
            block ? block() : nil;
        }
    }];
}

+ (void)presentAlertControllerWithTitle:(NSString *)title message:(NSString *)message doneBlock:(void (^)())block cancelBlock:(void (^)())cancelBlock {
    [self presentAlertControllerWithTitle:title message:message doneTitle:@"确定" cancelTitle:@"取消" completeBlock:^(BOOL isCancelled) {
        if (isCancelled) {
             cancelBlock ? cancelBlock() : nil;
        } else {
             block ? block() : nil;
        }
    }];
}

+ (void)presentAlertControllerWithTitle:(NSString *)title
                                message:(NSString *)message
               withTextFieldPlaceholder:(NSString *)placeholder
                              doneBlock:(void (^)(NSString *))block
                            cancelBlock:(void (^)())cancelBlock {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    __block UITextField *_textField;
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = placeholder;
        
        _textField = textField;
    }];
    
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        block(_textField.text);
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        cancelBlock();
    }];
    
    [alertController addAction:doneAction];
    [alertController addAction:cancelAction];
    
    [self presentAlertController:alertController];
}

+ (void)presentAlertControllerWithTitle:(NSString *)title
                                message:(NSString *)message
                              doneTitle:(NSString *)doneTitle
                            cancelTitle:(NSString *)cancelTitle
                          completeBlock:(void (^)(BOOL))completeBlock {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:doneTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        completeBlock ? completeBlock(NO) : nil;
    }];
    
    if (cancelTitle.length > 0) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
             completeBlock ? completeBlock(YES) : nil;
        }];
        [alertController addAction:cancelAction];
    }
    
    [alertController addAction:doneAction];
    
    [self presentAlertController:alertController];
}

+ (void)presentActionControllerWithTitle:(NSString *)title
                                 actions:(NSArray<UIAlertAction *> *)actions {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    for (UIAlertAction *action in actions) {
        [alertController addAction:action];
    }
    [[self findAppreciatedRootVC] presentViewController:alertController animated:YES completion:nil];
}


@end
